<?
# User Session class
class Session
{
	# Database
	private $DB;
	
	# Session
	private $SessionID;
	
	# User
	public $Online;
	public $Username;
	public $Email;
	public $Character;
	public $SubCharacters;
	public $Language;
	
	# Status
	private $Level;
	
	public function Session($Database)
	{
		$this->DB = $Database;
		
		// Try start session
		$this->Start();
	}
	
	public function ID()
	{
		return $this->SessionID;
	}

	#--------------------------------------------------------------------------------
	# Check for a login
	#--------------------------------------------------------------------------------
	
	// This method is run at the start to check if a user has logged in
	public $FormErrors = array();
	public function CheckLogin()
	{
		if ($_POST['newlogin'] == 'true')
		{
			# User Data
			if (empty($_POST['loginUser']))
				$this->FormErrors['user'] = "Username Empty";
				
			if (empty($_POST['loginPass']))
				$this->FormErrors['password'] = "Password Empty";
				
			# Check Credentials
			$CredientialCheck = $this->DB->GetData(TBL_USERS, TBL_USERS_USERNAME, false, TBL_USERS_USERNAME ."='". $_POST['loginUser'] ."' AND ". TBL_USERS_PASSWORD ."='". md5($_POST['loginPass']) ."'" );
	
			if ($CredientialCheck[0] == 0)
			{
				$this->FormErrors['user'] = "Incorrect Login";
				$this->FormErrors['pass'] = ' ';
			}
			
			# No Errors
			if (count($this->FormErrors) == 0)
			{
				// Generate Data
				$User	= trim($_POST["loginUser"]);
				$UserID = Random128();
				
				// Set Cookie
				setcookie('XIVPads[UserID]', $UserID, time()+(60*60*24*30*12));
				
				// Update database with session.
				$check = $this->DB->GetData(TBL_ACTIVE_USERS, "*", true, TBL_ACTIVE_USERS_USERNAME ."='". $User ."'");
				if ($check[0])
					$this->DB->Remove(TBL_ACTIVE_USERS, TBL_ACTIVE_USERS_USERNAME ."='". $User ."'");
					
				$this->DB->Update(TBL_USERS, array(TBL_USERS_ID ."='". $UserID ."'", 
												   TBL_USERS_TIMESTAMP ."='". time() ."'"), TBL_USERS_USERNAME ."='". $User ."'");
												   
				$this->DB->Insert(TBL_ACTIVE_USERS, array(TBL_ACTIVE_USERS_USERNAME ."='". $User ."'", 
												   		  TBL_ACTIVE_USERS_TIMESTAMP ."='". time() ."'"));
				
				$IP = GetIP();										  
				$Check = $this->DB->GetData(TBL_ACTIVE_GUESTS, "*", true, TBL_ACTIVE_GUESTS_IP ."='". $IP ."'");
				//Show($Check);
				//Show($IP);
				if ($Check[0])
					$this->DB->Remove(TBL_ACTIVE_GUESTS, TBL_ACTIVE_GUESTS_IP ."='". $IP ."'");
					
				return true;
			}
		}
		return false;
	}
	
	// Checks if logged in and updates time accordingly
	public function Refresh()
	{
		$this->SessionID = $_COOKIE['XIVPads']['UserID'];
		
		if ($this->Online && !empty($this->SessionID))
		{
			$test  = $this->DB->Update(TBL_ACTIVE_USERS, array(TBL_ACTIVE_USERS_TIMESTAMP ."='". time() ."'"), TBL_ACTIVE_USERS_USERNAME ."='". $this->Username ."'");
			$test2 = $this->DB->Update(TBL_USERS, array(TBL_USERS_TIMESTAMP ."='". time() ."'"), TBL_USERS_ID ."='". $this->SessionID ."'");
		}
		else
		{
			if (empty($_POST['newlogin']))
			{
				$IP = GetIP();
				$Check = $this->DB->GetData(TBL_ACTIVE_GUESTS, "*", true, TBL_ACTIVE_GUESTS_IP ."='". $IP ."'");
				if ($Check[0])
					$this->DB->Update(TBL_ACTIVE_GUESTS, array(TBL_ACTIVE_GUESTS_IP ."='". $IP ."'", TBL_ACTIVE_GUESTS_TIMESTAMP ."='". time() ."'"), TBL_ACTIVE_GUESTS_IP ."='". $IP ."'");
				else
					$this->DB->Insert(TBL_ACTIVE_GUESTS, array(TBL_ACTIVE_GUESTS_IP ."='". $IP ."'", TBL_ACTIVE_GUESTS_TIMESTAMP ."='". time() ."'"));
			}
		}
	}
	
	// Method to start a session
	public function Start()
	{
		$CookieID = $_COOKIE['XIVPads']['UserID'];
		
		if (!empty($CookieID))
		{
			$Fields = TBL_USERS_USERNAME .','.
					  TBL_USERS_ID .','.
					  TBL_USERS_CHARACTER .','.
					  TBL_USERS_LEVEL .','.
					  TBL_USERS_EMAIL .','.
					  TBL_USERS_TIMESTAMP .','.
					  TBL_USERS_SUBS .','.
					  TBL_USERS_LANGUAGE;
					  
			$UserData = $this->DB->GetData(TBL_USERS, $Fields, false, TBL_USERS_ID ."='". $CookieID ."'");
			
			if ($UserData[0])
			{
				$UserData = $UserData[3];
				
				if ($UserData[TBL_USERS_ID] == $_COOKIE['XIVPads']['UserID'])
				{
					$this->Online 		 = true;
					
					$this->Username 	 = $UserData[TBL_USERS_USERNAME];
					$this->Email 		 = $UserData[TBL_USERS_EMAIL];
					$this->Character 	 = $UserData[TBL_USERS_CHARACTER];
					$this->SubCharacters = $UserData[TBL_USERS_SUBS];
					$this->Language 	 = $UserData[TBL_USERS_LANGUAGE];
					
					$this->Level 		 = $UserData[TBL_USERS_LEVEL];
				}
			}
		}
	}
	
	// Method to login
	public function CheckLogout()
	{
		$URL_Data = DetectQuery();
	
		$page = $URL_Data[1];
	
		if ($page == 'x' && $this->Online)
		{
			$this->DB->Update(TBL_USERS, array(TBL_USERS_TIMESTAMP ."='". time() ."'"), TBL_USERS_ID ."='". $this->SessionID ."'");
			$this->DB->Remove(TBL_ACTIVE_USERS, TBL_ACTIVE_USERS_USERNAME ."='". $this->Username ."'");
			setcookie('XIVPads[UserID]', "", time()-(3600));
			return true;
		}
		return false;
	}
	
	#--------------------------------------------------------------------------------
	# Get User Data
	#--------------------------------------------------------------------------------
	
	// Method to return level status
	public function GetStatusText()
	{
		switch($this->Level)
		{
			case '1': return "Member";
			case '2': return "Member + Beta Tester";
			case '3': return "Moderator";
			case '4': return "Content Creator";
			case '9': return "Administrator";	
		}
	}
	
	public function isAdmin()
	{
		if ($this->Level == 9)
			return true;
		else
			return false;
	}
	
	#--------------------------------------------------------------------------------
	# Get character data
	#--------------------------------------------------------------------------------
	public function GetCharacter($Type)
	{
		$FieldsA = TBL_CHARACTERS_CICUID .','.
				  TBL_CHARACTERS_NAME .','.
				  TBL_CHARACTERS_CLEANNAME .','.
				  TBL_CHARACTERS_SERVER .','.
				  TBL_CHARACTERS_AVATAR .','.
				  TBL_CHARACTERS_LASTUPDATED .','.
				  TBL_CHARACTERS_BANNER .','.
				  TBL_CHARACTERS_POSITION .','.
				  TBL_CHARACTERS_LOGO .','.
				  TBL_CHARACTERS_VERSION;
		
		$FieldsB = array(TBL_STATS_PROFILE);
					  
		$Character 	= $this->DB->GetData(TBL_CHARACTERS, $FieldsA, false, TBL_CHARACTERS_CICUID ."='". $this->Character ."'");
		$Stats 		= $this->DB->GetData(TBL_STATS, $FieldsB, false, TBL_STATS_CICUID ."='". $this->Character ."'");

		if ($Character[0])
		{
			$Character 	= $Character[3];
			$Stats 		= $Stats[3];
			$Profile 	= explode(":", $Stats[TBL_STATS_PROFILE]);
			switch($Type)
			{
				case 'id': 			return $Character[TBL_CHARACTERS_CICUID];
				case 'name': 		return $Character[TBL_CHARACTERS_NAME];
				case 'cleanname':	return $Character[TBL_CHARACTERS_CLEANNAME];
				case 'server': 		return $Character[TBL_CHARACTERS_SERVER];
				case 'avatar': 		return $Character[TBL_CHARACTERS_AVATAR];
				case 'lastupdated': return $Character[TBL_CHARACTERS_LASTUPDATED];
				
				case 'banner': 		return $Character[TBL_CHARACTERS_BANNER];
				case 'position': 	return $Character[TBL_CHARACTERS_POSITION];
				case 'logo': 		return $Character[TBL_CHARACTERS_LOGO];
				case 'version':		return $Character[TBL_CHARACTERS_VERSION];
				
				case 'home':		return $Profile[2];
			}
		}
	}
	
}
?>